package cn.itcast.service;

import cn.itcast.dao.CheckItemDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
 *Author:TH
 *Dete:2019/10/2019:51
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    //添加检查项
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //分页查询
    public PageResult pageQuery(Integer cp, Integer ps, String qs) {
        PageHelper.startPage(cp,ps);
        Page<CheckItem> page = checkItemDao.findByCondition(qs);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //按照id删除
    public void delectById(Integer id) {
        //删除之前查询是否与项目相关
        long count = checkItemDao.findGroup(id);
        if (count>0){
            //有相关项目组,不可删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }else{
            //没有被引用,可以删除
            checkItemDao.delectById(id);
        }
    }

    //按照id查询
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }


    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }


    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
